import configs from '@/config/config'
import { colors } from '@/constants/injection-key'
import { useStore } from '@/store'
import storage from '@/utils/storage' // 缓存

// save token and then redirect to index
// 保存token并跳转
export async function saveTokenAndRefresh(accessToken: string) {
  const store = useStore()
  storage.setAccessToken(accessToken)
  const user = await store.fetchUserInfo()
  if (user.data.success) {
    let navigation =
      getCurrentPages()[getCurrentPages().length - getCurrentPages().length]
    console.log('navigation', navigation)
    if (getCurrentPages().length > 1) {
      if (navigation.route == 'pages/passport/login') {
        uni.switchTab({ url: '/pages/index' })
      } else {
        if (!navigation.route || navigation.route == 'undefined') {
          uni.switchTab({ url: '/pages/index' })
        } else {
          uni.navigateBack({
            delta: getCurrentPages().length
          })
        }
      }
    } else {
      uni.switchTab({
        url: '/pages/index'
      })
    }
  }
}
export const disabledStyle = `linear-gradient(90deg, ${colors.disabledLightColor} 2.21%, ${colors.disabledColor} 99.86%);`
export const fetchStyle = `linear-gradient(57.72deg, ${colors.lightColor} 18.14%, ${colors.themeColor} 98.44%);`
export const placeholderStyle =
  'font-size: 32rpx;line-height: 32rpx;color: #999999;'
export const mobileInputStyle = {
  background: configs.colors.bgColor,
  height: '100rpx',
  padding: '0 32rpx'
}
export const passwordInputStyle = {
  background: configs.colors.bgColor,
  height: '70rpx',
  padding: '0 32rpx'
}
